//
//  InviteFriendsViewController.m
//  SpendFunk
//
//  Created by KID on 5/7/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "InviteFriendsViewController.h"
#import "SpendFunkAppDelegate.h"

@implementation InviteFriendsViewController
@synthesize m_pArrContactList;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.


// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
    [super loadView];
    [self initView];
    [self initNotification];
}

-(void)initView
{
	/// backgrounnd for find friends screen
	NSString *path = [[NSString alloc] initWithFormat:@"%@",IMG_BKGR_VIEW];
	UIImage *img = [UIImage imageNamed:path];
	UIImageView *imgvFFBkgr = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
	[imgvFFBkgr setImage:img];
	[self.view addSubview:imgvFFBkgr];
	[imgvFFBkgr setUserInteractionEnabled:YES];
	[imgvFFBkgr release];
	////	
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_HEADER];
	img = [UIImage imageNamed:path];
	UIImageView *imgvBar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
	[imgvBar setImage:img];
	[self.view addSubview:imgvBar];
	[imgvBar setUserInteractionEnabled:YES];
	[imgvBar release];
	//back	
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BTN_BACK];
	img = [UIImage imageNamed:path];	
	m_pBtnBack = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, img.size.width, img.size.height)];
	[m_pBtnBack setBackgroundImage:img forState:UIControlStateNormal];
	[m_pBtnBack setBackgroundColor:[UIColor clearColor]];
	[m_pBtnBack setTitle:@"Back" forState:UIControlStateNormal];
	m_pBtnBack.titleLabel.textColor = [UIColor whiteColor];
	[m_pBtnBack addTarget:nil action:@selector(onClickBack) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:m_pBtnBack];
	[path release];
    
	path = [[NSString alloc] initWithFormat:@"%@",IMG_TEXT_INVITEFRIENDS];
	img = [UIImage imageNamed:path];
	UIImageView *imgvText = [[UIImageView alloc] initWithFrame:CGRectMake(320/2-img.size.width/2, 70, img.size.width, img.size.height)];
	[imgvText setImage:img];
	[self.view addSubview:imgvText];
	[imgvText setUserInteractionEnabled:YES];
	[imgvText release];
	
	int nHRow = 30;
	m_pTableInviteFriends = [[UITableView alloc] initWithFrame:CGRectMake(10,100, 300, 6*nHRow+15) style:UITableViewStyleGrouped];
	m_pTableInviteFriends.delegate =self;
	m_pTableInviteFriends.dataSource = self;
	m_pTableInviteFriends.rowHeight = nHRow;
	[m_pTableInviteFriends setShowsVerticalScrollIndicator:NO];
	[m_pTableInviteFriends setBackgroundColor:[UIColor clearColor]];
	m_pTableInviteFriends.scrollEnabled = YES;
    [self.view addSubview:m_pTableInviteFriends];
    [m_pTableInviteFriends release];
}

-(void)initNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setArray:) name:@"ListContact" object:nil];
}

-(void)setArray:(NSNotification*)arr
{
    m_pArrContactList = [arr object];
    [m_pTableInviteFriends reloadData];
}

-(void)onClickBack
{
	[g_AppDelegate openFindFriends];
}

#pragma mark tableViewdelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [m_pArrContactList count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
	cell.textLabel.text = [[m_pArrContactList objectAtIndex:indexPath.row] valueForKey:@"kName"];
    cell.textLabel.textColor = [UIColor colorWithRed:37.0f/255 green:46.0f/255 blue:72.0f/255 alpha:1];
    NSString *path = [[NSString alloc] initWithFormat:@"%@",IMG_ACCESSORY];
	UIImage *img = [UIImage imageNamed:path];
    [path release];
    UIImageView *pImageView = [[UIImageView alloc] initWithImage:img];
    [cell setAccessoryView:pImageView];
    
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [g_AppDelegate openDetailContact];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"DetailContact" object:[m_pArrContactList objectAtIndex:indexPath.row]];
}


/*
 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
 - (void)viewDidLoad {
 [super viewDidLoad];
 }
 */

/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations.
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
